package com.atguigu.juc.interview.thread.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author : leimingchao
 * @date Date : 2021年07月09日 20:07
 */
public class MyThreadPoolDemo {

    public static void main(String[] args) {
        //一池5个工作线程，类似一个银行有5个受理窗口
        //ExecutorService threadPool = Executors.newFixedThreadPool(5);
        //一池1个工作线程，类似一个银行有1个受理窗口
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //一池N个工作线程，类似一个银行有N个受理窗口
        ExecutorService threadPool = Executors.newCachedThreadPool();
        try {
            for (int i = 1; i <= 10; i++) {
                //暂停
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                threadPool.submit(()-> System.out.println(Thread.currentThread().getName() + "\t 办理业务"));
                //暂停
                // try {
                //     TimeUnit.SECONDS.sleep(1);
                // } catch (InterruptedException e) {
                //     e.printStackTrace();
                // }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
